package cn.ptu.pmaa.controller;

import cn.ptu.pmaa.entities.RespBean;
import cn.ptu.pmaa.service.LoginRegService;
import cn.ptu.pmaa.utils.DateUtil;
import cn.ptu.pmaa.utils.EmailUtil;
import cn.ptu.pmaa.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.*;

@RestController
public class LoginRegController {

    @Autowired
    EmailUtil emailUtil;
    @Autowired
    TokenUtil tokenUtil;
    @Autowired
    LoginRegService loginRegService;
    @Autowired
    DateUtil dateUtil;

//    @RequestMapping(value = "/login")
//    public RespBean ttt(HttpServletRequest httpServletRequest){
//        Map<String,String[]> map=httpServletRequest.getParameterMap();
//        System.out.println("333");
//        //遍历
//        for(Iterator iter=map.entrySet().iterator();iter.hasNext();){
//            Map.Entry element=(Map.Entry)iter.next();
//            //key值
//            Object strKey = element.getKey();
//            //value,数组形式
//            String[] value=(String[])element.getValue();
//
//            System.out.print(strKey.toString() +"=");
//            for(int i=0;i<value.length;i++){
//                System.out.print(value[i]+",");
//            }
//            //System.out.println();
//        }
//        return new RespBean("404","登录失败");
//    }

    @RequestMapping(value = "/login")
    @CrossOrigin
    public RespBean login(@RequestParam String id, @RequestParam String pwd,@RequestParam String role){
        int result = loginRegService.login(id,pwd,role);
        System.out.println(id+pwd+role);
        if (result == 1) {
            HashMap<String,Object> data = new HashMap<>();
            data.put("token",tokenUtil.getToken(id,role));
            System.out.println(200);
            return new RespBean("200","登录成功",data);
        }else {
            System.out.println(400);
            return new RespBean("404","登录失败");
        }
    }

    @RequestMapping(value = "/logout")
    public RespBean logout(HttpServletRequest request){
        return new RespBean("200","退出登录成功");
    }

    @RequestMapping("/updatepwd")
    public RespBean updatePwd(@RequestParam String id, @RequestParam String role){
        int flag = loginRegService.updatePwd(id,role);
        if (flag==1){
            return new RespBean("200","发送成功");
        }else {
            return new RespBean("400","无此账号");
        }


    }

    @RequestMapping("/checkyzm")
    public RespBean checkYzm(@RequestParam("yzm") String yzm, @RequestParam("id") String id,@RequestParam("role") String role,@RequestParam("pass") String pass,HttpServletRequest httpServletRequest) throws ParseException {
        Date senddate = dateUtil.myStringToDateTime(String.valueOf(httpServletRequest.getSession().getAttribute("sendtime")));
        //httpServletRequest.get
        if (emailUtil.getMinute(senddate ,new Date())>1){
            return new RespBean("500","超时了");
        }else {
            if (httpServletRequest.getSession().getAttribute("yzm").equals(yzm)){
                if (loginRegService.checkYzm(id,role,pass)==1){
                    return new RespBean("200","成攻");
                }else
                    return new RespBean("200","修改失败");
            }else {
                return new RespBean("200","yzm不对");
            }
        }
    }

//    @RequestMapping("/register")
//    public RespBean register(@RequestParam String id, @RequestParam String pwd){
//        int result = loginRegService.register(id,pwd);
//        if (result == 0) {
//            //成功
//            return new RespBean("success", "注册成功!");
//        } else if (result == 1) {
//            return new RespBean("error", "用户名重复，注册失败!");
//        } else {
//            //失败
//            return new RespBean("error", "注册失败!");
//        }
//    }

}
